package com.alks.function.controller.finance.ryfinance;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.PcCostTemp;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.finance.ryfinance.CostDetailDto;
import com.alks.function.data.request.finance.ryfinance.*;
import com.alks.function.service.finance.ryfinance.IBillFessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@ApiRestController("/ryFinance")
@Api(value = "财务部", tags = "财务部-荣阳财务-费用结算作业")
@Slf4j
public class BillFessController {
    @Autowired
    private IBillFessService service;

    @WebLogger
    @GetMapping("/costTempQuery")
    @ApiOperation(value = "费用明细输入查询-查询", tags = "财务部-荣阳财务-费用结算作业")
    public ResponseInfo costTempQuery() {
        return service.costTempQuery();
    }

    @WebLogger
    @PostMapping("/costTempAdd")
    @ApiOperation(value = "费用明细输入新增-新增", tags = "财务部-荣阳财务-费用结算作业")
    public ResponseInfo costTempAdd(@RequestBody PcCostTemp request) {
        return service.costTempAdd(request);
    }

    @WebLogger
    @PutMapping("/costTempUpdate")
    @ApiOperation(value = "费用明细输入数据修改-更新", tags = "财务部-荣阳财务-费用结算作业")
    public ResponseInfo costTempUpdate(@RequestBody PcCostTemp request) {
        service.costTempUpdate(request);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PutMapping("/costTempDelete")
    @ApiOperation(value = "费用明细输入数据删除-删除", tags = "财务部-荣阳财务-费用结算作业")
    public ResponseInfo costTempDelete(@RequestParam @ApiParam("序号") Integer item) {
        return service.costTempDelete(item);
    }

    @PostMapping("/downloadDetailQuery")
    @WebLogger
    @ApiOperation(value = "导出", tags = "财务部-荣阳财务-费用结算作业")
    public void  downloadCutYield(HttpServletResponse response, @RequestBody CostDetailQueryRequest request) throws IOException {
        request.setPageSize(-1);
        Map<String, Object> map  = service.costDetailQuery(request);
        List<CostDetailDto> dtos = (List<CostDetailDto>) map.get("dtos");
        dtos = new ArrayList<>(dtos);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());
        BigDecimal total = dtos.stream().map(CostDetailDto::getAmt).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
        CostDetailDto dto = new CostDetailDto();
        dto.setAmt(total);
        dto.setLotNo("合计");
        dtos.add(dto);
        ExcelUtils.creatByTemplate(response,"费用结算作业模板.xls",date,dtos);
    }

    @WebLogger
    @PostMapping("/costDetailQuery")
    @ApiOperation(value = "费用查询明细-分页可条件查询", tags = "财务部-荣阳财务-费用结算作业")
    public ResponseInfo costDetailQuery(@RequestBody CostDetailQueryRequest request) {
        Map<String, Object> map = service.costDetailQuery(request);
        return ResponseInfo.ok(map);
    }

    @WebLogger
    @PostMapping("/costTempToDetail")
    @ApiOperation(value = "费用明细增加-新增，删除", tags = "财务部-荣阳财务-费用结算作业")
    public ResponseInfo costTempToDetail(@RequestBody CostTempToDetailRequest request) {
        return service.costTempToDetail(request);
    }

    @WebLogger
    @PostMapping("/costDetailToTemp")
    @ApiOperation(value = "费用明细修改/扣款-新增，删除", tags = "财务部-荣阳财务-费用结算作业")
    public ResponseInfo costDetailToTemp(@RequestBody List<CostDTTRequest> request,
                                         @RequestParam @ApiParam(allowEmptyValue = true, value = "1.修改 2.指定扣款") Integer type) {
        return service.costDetailToTemp(request, type);
    }

    @WebLogger
    @PutMapping("/costDetailUp")
    @ApiOperation(value = "费用明细更新(包括结算)-更新", tags = "财务部-荣阳财务-费用结算作业")
    public ResponseInfo costDetailUpdate(@RequestBody List<CostDetUpRequest> request,
                                         @RequestParam(required = false) @ApiParam(value = "1.结算 2.取消结算", required = true) Integer financeType,
                                         @RequestParam(required = false) @ApiParam("结算月份") String financeYymm) {
        return service.costDetailUpdate(request, financeType, financeYymm);
    }

    @WebLogger
    @PostMapping("/costDetailDelete")
    @ApiOperation(value = "费用明细删除-删除，更新", tags = "财务部-荣阳财务-费用结算作业")
    public ResponseInfo costDetailDelete(@RequestParam @ApiParam(required = true,value = "序号") BigDecimal item,
                                         @RequestParam @ApiParam(required = true,value = "订单号") String slipNo) {
        return service.costDetailDelete(slipNo,item);
    }

}
